package backend;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;

import beans.CharBean;
import beans.ProbenBean;
import beans.TawBean;
import beans.VerlaufElement;

public class Daten {

	private HashMap<Integer, CharBean> charList = new HashMap<Integer, CharBean>();
	private HashMap<Integer, ProbenBean> probenList = new HashMap<Integer, ProbenBean>();
	private LinkedList<String> attributList = new LinkedList<String>();
	private LinkedList<VerlaufElement> verlauf = new LinkedList<VerlaufElement>();

	public Daten() {

		/**
		 * AttributLsite erstellen
		 * 
		 */
		attributList.add("MU");
		attributList.add("KL");
		attributList.add("IN");
		attributList.add("CH");
		attributList.add("FF");
		attributList.add("GE");
		attributList.add("KO");
		attributList.add("KK");
	}

	public void addChar(CharBean character) {

		synchronized (charList) {
			charList.put(character.getCharId(), character);
		}
	}

	public void addProbe(ProbenBean probe) {

		synchronized (probenList) {
			probenList.put(probe.getId(), probe);
		}
	}

	public void addToVerlauf(String type, String data) {

		VerlaufElement newElement = new VerlaufElement(type, data);
		verlauf.addFirst(newElement);
	}

	public void addToVerlauf(VerlaufElement element) {

		verlauf.addFirst(element);
	}

	public void allesLaden(File file) throws FileNotFoundException {

		if (file == null) {
			return;
		}
		Scanner scanner = new Scanner(file);
		String line = null;
		CharBean charaToAdd = null;
		ProbenBean probeToAdd = null;
		clearCharList();
		clearProbenList();
		while (scanner.hasNextLine()) {
			line = scanner.nextLine();
			String parts[] = line.split("\\|\\$\\|");
			if (parts[1].equals("CharProben")) {
				int id = Integer.parseInt(parts[2]);
				CharBean chara2 = this.getCharById(id);
				int i = 0;
				for (String string : parts) {
					if (i > 2) {
						String parts2[] = string.split("\\|");
						chara2.addToProbenList(Integer.parseInt(parts2[0]),
									Integer.parseInt(parts2[1]));
					}
					i++;
				}
			} else if (parts[1].equals("char")) {
				String name = parts[3];
				try {
					int id = Integer.parseInt(parts[2]);
					int mu = Integer.parseInt(parts[4]);
					int kl = Integer.parseInt(parts[5]);
					int in = Integer.parseInt(parts[6]);
					int ch = Integer.parseInt(parts[7]);
					int ff = Integer.parseInt(parts[8]);
					int ge = Integer.parseInt(parts[9]);
					int ko = Integer.parseInt(parts[10]);
					int kk = Integer.parseInt(parts[11]);
					int at = Integer.parseInt(parts[12]);
					int pa = Integer.parseInt(parts[13]);
					charaToAdd = new CharBean(id, name, mu, kl, in, ch, ff, ge,
								ko, kk, at, pa);
					addChar(charaToAdd);
				} catch (NumberFormatException ex) {
					System.out
								.println("Es müssen für die Werte ganze Zahlen eingegeben werden!");
				}
			} else if (parts[1].equals("proben")) {
				String name = parts[2];
				try {
					int id = Integer.parseInt(parts[3]);
					probeToAdd = new ProbenBean(id, name, parts[4], parts[5],
								parts[6]);
					addProbe(probeToAdd);
				} catch (NumberFormatException ex) {
					System.out
								.println("Es müssen für die Werte ganze Zahlen eingegeben werden!");
				}
			} else if (parts[1].equals("verlauf")) {
				System.out.println("Verlaufelement gefunden");
			} else {
				System.out.println("Daten nicht bekannt");
			}
		}
	}

	public void allesSpeichern(File file) {

		if (file == null) {
			return;
		}
		Writer fw = null;
		try {
			fw = new FileWriter(file);
			for (CharBean chara : charList.values()) {
				fw.write("|$|char|$|" + chara.getCharId() + "|$|"
							+ chara.getName() + "|$|" + chara.getMU() + "|$|"
							+ chara.getKL() + "|$|" + chara.getIN() + "|$|"
							+ chara.getCH() + "|$|" + chara.getFF() + "|$|"
							+ chara.getGE() + "|$|" + chara.getKO() + "|$|"
							+ chara.getKK() + "|$|" + chara.getAttacke()
							+ "|$|" + chara.getParade() + "|$|");
				fw.append('\n');
				ArrayList<TawBean> _probenList = chara.getProbenList();
				fw.write("|$|CharProben|$|" + chara.getCharId() + "|$|");
				for (TawBean probe : _probenList) {
					fw
								.write(probe.getProbenId() + "|"
											+ probe.getTaw() + "|$|");
				}
				fw.append('\n');
			}
			for (ProbenBean probe : probenList.values()) {
				fw
							.write("|$|proben|$|" + probe.getName() + "|$|"
										+ probe.getId() + "|$|"
										+ probe.getAtt1() + "|$|"
										+ probe.getAtt2() + "|$|"
										+ probe.getAtt3() + "|$|");
				fw.append('\n');
			}
		} catch (IOException e) {
			System.err.println("Konnte Datei nicht erstellen");
		} finally {
			if (fw != null) {
				try {
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private void clearCharList() {

		synchronized (charList) {
			charList.clear();
		}
	}

	public void clearProbenList() {

		synchronized (probenList) {
			probenList.clear();
		}
	}

	public int getAttributId(String att) {

		return attributList.indexOf(att);
	}

	public String getAttributString(int index) {

		return attributList.get(index);
	}

	public CharBean getCharById(Integer id) {

		return charList.get(id);
	}

	public CharBean getCharByName(String name) {

		for (CharBean chara : charList.values()) {
			if (name.equals(chara.getName())) {
				return chara;
			}
		}
		return null;
	}

	public HashMap<Integer, CharBean> getCharList() {

		synchronized (charList) {
			return charList;
		}
	}

	public int getNewCharId() {

		int id = charList.size();
		id++;
		return id;
	}

	public int getNewProbenId() {

		int id = probenList.size();
		id++;
		return id;
	}

	public ProbenBean getProbeById(Integer id) {

		ProbenBean probe = probenList.get(id);
		return probe;
	}

	public int getProbenIdByName(String name) {

		for (ProbenBean probe : probenList.values()) {
			if (probe.getName().equals(name)) {
				return probe.getId();
			}
		}
		return 0;
	}

	public HashMap<Integer, ProbenBean> getProbenList() {

		return probenList;
	}

	public LinkedList<VerlaufElement> getVerlauf() {

		return verlauf;
	}

	public void removeChar(Integer id) {

		synchronized (charList) {
			charList.remove(id);
		}
	}

	public void removeProbe(Integer id) {

		synchronized (probenList) {
			probenList.remove(id);
			for (CharBean chara : charList.values()) {
				ArrayList<TawBean> probenCharList = chara.getProbenList();
				for (TawBean tawBean : probenCharList) {
					if (tawBean.getProbenId() == id) {
						probenCharList.remove(tawBean);
						break;
					}
				}
			}
		}
	}

	public void updateChar(CharBean chara) {

		synchronized (charList) {
			charList.remove(chara.getCharId());
			charList.put(chara.getCharId(), chara);
		}
	}

	public void updateProbe(Integer id, ProbenBean probe) {

		synchronized (probenList) {
			probenList.remove(id);
			probenList.put(id, probe);
		}
	}
}
